package com.cloud.drore.eboss.supplier.mapper.provider;

import com.cloud.drore.eboos.common.util.DateToStringUtil;
import com.cloud.drore.eboss.supplier.entity.ProductStock;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/3/12 11:40 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class ProductStockProvider {

    public String updateProductStockByParam(ProductStock productStock){
        int count = 0;
        String sql = " update eboos_product_stock set ";
        if (StringUtils.isNotBlank(productStock.getInventoryType())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " inventory_type ='" + productStock.getInventoryType() + "'";
            count++;
        }
        if (productStock.getInventory()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " inventory ='" + productStock.getInventory() + "'";
            count++;
        }
        if (productStock.getStartTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " start_time ='" + productStock.getStartTime() + "'";
            count++;
        }
        if (productStock.getEndTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " end_time ='" + productStock.getEndTime() + "'";
            count++;
        }
        if (productStock.getDistributorId()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " distributor_id ='" + productStock.getDistributorId() + "'";
            count++;
        }
        sql += " where common_product_id= " + productStock.getCommonProductId() ;
        return sql;
    }

    private static String findProductStockByParamSql = " select * from `eboos_product_stock` " +
            " where is_deleted = 0 ";

    public String findProductStockByParam(ProductStock productStock){
        String sql =findProductStockByParamSql;
        if(StringUtils.isNotBlank(productStock.getInventoryType()))
            sql += " and inventory_type = '" + productStock.getInventoryType() + "'";
        if(productStock.getCommonProductId() != null)
            sql += " and common_product_id =" + productStock.getCommonProductId() + "";
        if(productStock.getDistributorId() != null)
            sql += " and distributor_id =" + productStock.getDistributorId() + "";
        if(productStock.getStartTime()!=null)
            sql += " AND '"+ DateToStringUtil.getStringDateTime(productStock.getStartTime())+"' > STR_TO_DATE(DATE_FORMAT(start_time,'%Y-%m-%d'),'%Y-%m-%d %H:%i:%s') " +
                    " AND '"+DateToStringUtil.getStringDateTime(productStock.getStartTime())+"' < DATE_ADD(DATE_ADD(STR_TO_DATE(DATE_FORMAT(start_time,'%Y-%m-%d'),'%Y-%m-%d %H:%i:%s'),INTERVAL 1 DAY),INTERVAL -1 SECOND)";
        return sql;
    }

}